<template>
  <el-row>
      <el-col :span="4">
        <h5 style="text-align: center">系统菜单</h5>
        <el-menu
            default-active="2"
            @select="menuSelect"
        >
            <template v-for="menu in menus">
                <el-menu-item :index="menu.menuRouter" v-if="menu.menuParent == 1">
                  <template #title>
                    <span>{{menu.menuText}}</span>
                  </template>
                </el-menu-item>

                <el-sub-menu v-else :index="menu.menuId" >
                  <template #title>
                    <span>{{menu.menuText}}</span>
                  </template>
                  <el-menu-item-group>
                    <!-- 遍历显示子菜单 -->
                    <!-- index直接绑定路由 -->
                    <el-menu-item v-for="subMenu in menu.children" :index="subMenu.menuRouter">
                      {{subMenu.menuText}}
                    </el-menu-item>
                  </el-menu-item-group>
                </el-sub-menu>
            </template>

        </el-menu>
      </el-col>

    <!--右边主题-->
      <el-col :span="20">
          <router-view/>
      </el-col>
  </el-row>
</template>

<script setup>
  import {onMounted, ref} from "vue";
  //导入homeRequest
  import homeRequest from "@/api/home.js";
  import router from "@/router/index.js";
  //定义数组保存菜单信息
  let menus = ref([]);
  //定义函数加载所有菜单信息
  function loadMenus(){
    homeRequest.loadMenus(res=>{
      menus.value = res.data;
    })
  }

  function menuSelect(index){
    console.log(index);
    router.push({path:index});
  }


  onMounted(()=>{
    loadMenus();
  })

</script>

<style scoped>

</style>